package dt.board.notice;

import com.opensymphony.xwork2.ActionSupport; 
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder; 

import dt.board.qna.qnaBoardVO;

import java.io.Reader;
import java.io.IOException; 
import java.net.URLEncoder; 
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;


public class noticeViewAction extends ActionSupport implements SessionAware{
	private Map session;
	public static Reader reader; 
	public static SqlMapClient sqlMapper; 
	
	private noticeBoardVO paramClass = new noticeBoardVO(); 
	private noticeBoardVO resultClass = new noticeBoardVO(); 
	
	private int currentPage; 
	private int no; 
	private String password;
	private int adminCheck;
	
	private int BoardFlag = 1;
	
	public noticeViewAction() throws IOException{
		
		reader = Resources.getResourceAsReader("sqlMapConfig.xml");
		sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
		reader.close();
		
	}
	
	public String execute() throws Exception{
		
		paramClass.setNo(getNo());
		sqlMapper.update("notice.updateReadHit",paramClass);
		
		resultClass = (noticeBoardVO) sqlMapper.queryForObject("notice.selectOne", getNo());
		
		
        if (session.get("id") != null ) {
	    	
	    	if(session.get("id").equals("admin1")){
	    		adminCheck = 1; 
	    	} 
	    }
		
		return SUCCESS;
		
	}
	
	public String checkForm() throws Exception {

		return SUCCESS;
	}

	// 비밀번호 체크 액션
	public String checkAction() throws Exception {

		// 비밀번호 입력값 파라미터 설정.
		paramClass.setNo(getNo());
		paramClass.setPassword(getPassword());

		// 현재 글의 비밀번호 가져오기.
		resultClass = (noticeBoardVO) sqlMapper.queryForObject("notice.selectPassword", paramClass);

		// 입력한 비밀번호가 틀리면 ERROR 리턴.
		if (resultClass == null){
			System.out.println("error");	
			return ERROR;}
		

		return SUCCESS;
		
	}

	public noticeBoardVO getParamClass() {
		return paramClass;
	}

	public void setParamClass(noticeBoardVO paramClass) {
		this.paramClass = paramClass;
	}

	public noticeBoardVO getResultClass() {
		return resultClass;
	}

	public void setResultClass(noticeBoardVO resultClass) {
		this.resultClass = resultClass;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public int getAdminCheck() {
		return adminCheck;
	}

	public void setAdminCheck(int adminCheck) {
		this.adminCheck = adminCheck;
	}

	public int getBoardFlag() {
		return BoardFlag;
	}

	public void setBoardFlag(int boardFlag) {
		BoardFlag = boardFlag;
	}
	
	
}
